package com.twp.auth.repository.auth;


import com.twp.auth.domain.authority.Role;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Set;

@Repository
public interface RoleRepository extends JpaRepository<Role, Integer>, JpaSpecificationExecutor<Role> {

    @Query(value = "SELECT r.id as id,roleName,r.updateTime as updateTime,r.updateUser as updateUser,roleCode,status from twp_role r ,twp_role_user u where r.id = u.roleId and u.manageUserId=:uid order by r.id", nativeQuery = true)
    List<Role> queryRolesByUid(@Param("uid") int uid);

    Role findByRoleName(String roleName);

    Role findByRoleCode(String code);
  //  Role findByPrimaryRole(Boolean primaryRole);


    // 差集删除
    @Query(
            nativeQuery = true,
            value = "DELETE FROM twp_role_menu WHERE menuId in ?1"
    )
    void deleteByDiff(List<Integer> ids);


    @Query(
            nativeQuery = true,
            value = "SELECT DISTINCT role.* FROM twp_role role " +
                    " INNER JOIN twp_role_menu role_menu ON role.id = role_menu.roleId" +
                    " INNER JOIN twp_menu menu ON menu.id = role_menu.menuId WHERE menu.id in ?1"
    )
    Set<Role> findAllByMenuIds(List<Integer> menuIds);
}